// 创建并显示全屏加载提示
function showFullscreenLoading(message = '分发服务正在检测抖音登录状态...,请勿关闭页面',isSuccessMessage = false) {
  //console.log('Showing loading overlay with message:', message);
  
  // 先移除已存在的遮罩层
  hideFullscreenLoading();
  
  // 创建遮罩层
  const overlay = document.createElement('div');
  overlay.id = 'dso-loading-overlay';
  overlay.style.cssText = `
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 999999;
  `;

  // 创建提示框
  const messageBox = document.createElement('div');
  messageBox.style.cssText = `
    background: white;
    padding: 20px 40px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    text-align: center;
    max-width: 80%;
  `;

  // 判断是否是成功消息
  // const isSuccessMessage = message.includes('可以关闭了');

  // 只在非成功消息时添加加载动画
  if (!isSuccessMessage) {
    // 添加加载动画
    const spinner = document.createElement('div');
    spinner.style.cssText = `
      border: 4px solid #f3f3f3;
      border-top: 4px solid #3498db;
      border-radius: 50%;
      width: 40px;
      height: 40px;
      animation: spin 1s linear infinite;
      margin: 0 auto 15px;
    `;

    // 添加动画关键帧
    const style = document.createElement('style');
    style.textContent = `
      @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
      }
    `;
    document.head.appendChild(style);
    messageBox.appendChild(spinner);
  }

  // 添加提示文本
  const text = document.createElement('p');
  text.style.cssText = `
    margin: 0;
    font-size: 16px;
    color: #333;
    font-family: Arial, sans-serif;
    ${isSuccessMessage ? 'margin-top: 5px;' : ''}
  `;
  text.textContent = message;

  // 组装DOM
  messageBox.appendChild(text);
  overlay.appendChild(messageBox);
  document.body.appendChild(overlay);

  // 如果是成功消息，3秒后自动关闭
  if (isSuccessMessage) {
    setTimeout(() => {
      hideFullscreenLoading();
    }, 3000);
  }
}

// 移除全屏加载提示
function hideFullscreenLoading() {
  const existingOverlay = document.getElementById('dso-loading-overlay');
  if (existingOverlay) {
    existingOverlay.remove();
  }
}

// 使用新的消息传递机制
function showLoading() {
  // 直接调用显示加载提示
  showFullscreenLoading('分发服务正在检测抖音登录状态...,请勿关闭页面');
}

function hideLoading() {
  hideFullscreenLoading();
}

// 修改消息监听器以匹配 background.js 发送的消息格式
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  //console.log('Content script received message:', request);
  
  if (request.action === 'showLoading') {
    showFullscreenLoading(request.message || '分发服务正在检测抖音登录状态...,请勿关闭页面');
    sendResponse({ status: 'success' });
  } else if (request.action === 'hideLoading') {
    // 处理错误消息显示
    if (request.message) {
      showFullscreenLoading(request.message,true);
      // 错误消息显示3秒后自动关闭
      setTimeout(() => {
        hideFullscreenLoading();
      }, 3000);
    } else {
      hideFullscreenLoading();
    }
    sendResponse({ status: 'success' });
  } else if (request.type === 'loadingStatus') {
    // 保留原有的 loadingStatus 处理逻辑
    if (request.isLoading) {
      showFullscreenLoading(request.message || '分发服务正在检测抖音登录状态...,请勿关闭页面');
    } else {
      if (request.message) {
        showFullscreenLoading(request.message);
        setTimeout(() => {
          hideFullscreenLoading();
        }, 3000);
      } else {
        hideFullscreenLoading();
      }
    }
  }
  
  return true;
});

// 导出函数供其他文件使用
window.showLoading = showLoading;
window.hideLoading = hideLoading; 